import 'dart:convert';

import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:word_app/app/modules/collect/views/collect_view.dart';
import 'package:word_app/app/modules/login/views/login_view.dart';
import 'package:word_app/app/modules/mine/views/mine_view.dart';
import 'package:word_app/app/modules/model/storage_model.dart';
import 'package:word_app/app/modules/model/user_model.dart';
import 'package:word_app/app/modules/myHome/views/my_home_view.dart';
import 'package:word_app/app/modules/vocabulary/views/vocabulary_view.dart';

class TabbarVcController extends GetxController {
  final List<Widget> pages = const [
    MyHomeView(),
    VocabularyView(),
    CollectView(),
    MineView()
  ];
  final List<BottomNavigationBarItem> items = [
    BottomNavigationBarItem(
        icon: Image.asset(
          "images/home.png",
          width: 25,
          height: 25,
        ),
        activeIcon: Image.asset(
          "images/homeSelect.png",
          width: 25,
          height: 25,
        ),
        label: "首页"),
    BottomNavigationBarItem(
        icon: Image.asset(
          "images/collect.png",
          width: 25,
          height: 25,
        ),
        activeIcon: Image.asset(
          "images/collectSelect.png",
          width: 25,
          height: 25,
        ),
        label: "词库"),
    BottomNavigationBarItem(
        icon: Image.asset(
          "images/vocabulary.png",
          width: 25,
          height: 25,
        ),
        activeIcon: Image.asset(
          "images/vocabularySelect.png",
          width: 25,
          height: 25,
        ),
        label: "收藏"),
    BottomNavigationBarItem(
        icon: Image.asset(
          "images/mine.png",
          width: 25,
          height: 25,
        ),
        activeIcon: Image.asset(
          "images/mineSelect.png",
          width: 25,
          height: 25,
        ),
        label: "我的")
  ];
  RxInt currentIndex = 0.obs;
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    loginOut();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void setCurrentindex(index) {
    currentIndex.value = index;
    update();
  }

  loginOut() {
    var usermodelJson = StorageModel.getUserModelData("userAllData");
    if (usermodelJson == null) {
      Get.off(const LoginView());
    } else {
      Map<String, dynamic> userMap = jsonDecode(usermodelJson);
      var usermodel = UserModel.fromJson(userMap);
      MyUserData.userModel = usermodel;
      if (usermodel.data?.token == null) {
        Get.off(const LoginView());
      }
    }
  }
}
